using System;
using System.Collections;
using System.IO;
using System.Web.UI.WebControls;
using Portal.Core;
using Portal.Core.Utils;
using Portal.Data;
using Page = System.Web.UI.Page;

public partial class Admin_FileManager : Page
{
    private const string Directory = "Themes";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (PortalContext.CurrentUser != null)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["edit"]))
                {
                    divEdit.Visible = true;
                    txtpath.Text = Session["edit@path"].ToString();
                    try
                    {
                        ContentEdit.Text = MyFile.ReadTextFromFile(txtpath.Text);
                    }
                    catch
                    {
                    }
                }
                else
                    divEdit.Visible = false;
            }
            else
            {
                Response.Redirect(CoreContext.basepath + "admin_login.aspx");
            }
        }
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int index = Convert.ToInt32(e.CommandArgument);
        GridViewRow row = GridView1.Rows[index];

        string path = Server.HtmlDecode(row.Cells[0].Text);


        switch (e.CommandName)
        {
            case "MyEdit":
                Session["edit@path"] = path;
                Response.Redirect(CoreContext.basepath+"Admin/FileManager.aspx?edit=1");
                break;
            case "MyDelete":
                File.Delete(path);
                break;
        }

        LoadItems();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        LoadItems();
    }

    private void LoadItems()
    {
        divEdit.Visible = false;
        string ext = DropDownList1.SelectedValue;
        SearchStuff(Directory, ext);
    }

    private void SearchStuff(string Folder, string extensionFilter)
    {
        DirectoryInfo difo = new DirectoryInfo(CoreContext.physicalStaticFile +Folder);

        FileInfo[] listFileInfo = difo.GetFiles("*." + extensionFilter, SearchOption.AllDirectories);

        IList listItems = new ArrayList();
        if (listFileInfo.Length > 0)
        {
            for (int i = 0; i < listFileInfo.Length; i++)
            {
                MyObjectFile file = new MyObjectFile();
                file.FileName = listFileInfo[i].Name;
                file.Path = listFileInfo[i].FullName;


                listItems.Add(file);
            }
            GridView1.DataSource = listItems;
        }

        GridView1.DataBind();
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        //save
        MyFile.WriteFile(txtpath.Text, ContentEdit.Text, true);
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        LoadItems();
    }

    protected void Button4_Click(object sender, EventArgs e)
    {
        //create file
        Session["edit@path"] = CoreContext.physicalStaticFile +Directory;
        Response.Redirect(CoreContext.basepath + "FileManager.aspx?edit=1");
    }

    #region Nested type: MyObjectFile

    public class MyObjectFile
    {
        private string _path;
        public string Path
        {
            get { return _path; }
            set { _path = value; }
        }

        private string _fileName;
        public string FileName
        {
            get { return _fileName; }
            set { _fileName = value; }
        }

        public string Content
        {
            get
            {
                string sContent = string.Empty;
                try
                {
                    sContent = MyFile.ReadTextFromFile(Path);
                }
                catch
                {
                }

                return sContent;
            }
        }
    }

    #endregion
}